﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using fanfu.Model;
using fanfu.Common;
using System.Data;
using fanfu.Business;

public partial class Index : BasePage
{
    DataTable dataList = new DataTable();
    int page;
    string requestType = string.Empty;
    string searchKeyword = string.Empty;
    string articleTag = string.Empty;
    string archives = string.Empty;

    /// <summary>
    /// 页面加载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        string sqlString = string.Empty;
        try
        {
            requestType = Request["type"].ToString();
            page = Convert.ToInt32(Request["page"].ToString());
            if (page <= 0) //如果输入的页数小于等于0则跳转到第一页
                page = 1;
        }
        catch (Exception ex)
        {
            requestType = "index";
            page = 1;
        }

        #region 判断请求类型
        int topValue = __PageSize * (page - 1);
        switch (requestType)
        {
            case "index":
                sqlString = "SELECT TOP " + __PageSize + " * FROM Article WHERE ID NOT IN (SELECT TOP " + topValue + " ID FROM Article WHERE Flag=" + (int)ArticleFlag.normal + " ORDER BY SetTop, CreateTime DESC, ID DESC) AND Flag=" + (int)ArticleFlag.normal + " ORDER BY SetTop, CreateTime DESC, ID DESC";
                dataList = new ArticleRule().GetAllNormalFlagArticleInfo();
                this.paging.InnerHtml = CreatePaging(dataList.Rows.Count, page, "Index.aspx?current=0&type=index");
                break;

            case "articleList":
                sqlString = "SELECT TOP " + __PageSize + " * FROM Article WHERE ID NOT IN (SELECT TOP " + topValue + " ID FROM Article WHERE Flag=" + (int)ArticleFlag.normal + " ORDER BY SetTop, CreateTime DESC, ID DESC) AND Flag=" + (int)ArticleFlag.normal + " ORDER BY SetTop, CreateTime DESC, ID DESC";
                dataList = new ArticleRule().GetAllNormalFlagArticleInfo();
                this.paging.InnerHtml = CreatePaging(dataList.Rows.Count, page, "Index.aspx?current=0&type=articleList");
                break;

            case "search":
                try
                {
                    searchKeyword = Request["keyword"].ToString();
                }
                catch (Exception ex)
                {
                    searchKeyword = "搜索结果为空";
                }
                sqlString = "SELECT TOP " + __PageSize + " * FROM Article WHERE ID NOT IN (SELECT TOP " + topValue + " ID FROM Article WHERE Flag=" + (int)ArticleFlag.normal + " AND Caption LIKE '%" + searchKeyword + "%' ORDER BY SetTop, CreateTime DESC, ID DESC) AND Flag=" + (int)ArticleFlag.normal + " AND Caption LIKE '%" + searchKeyword + "%' ORDER BY SetTop, CreateTime DESC, ID DESC";
                string searchCount = "SELECT ID FROM Article WHERE Flag=" + (int)ArticleFlag.normal + " AND Caption LIKE '%" + searchKeyword + "%'";
                dataList = SqlHelper.GetDataTable(searchCount);
                this.paging.InnerHtml = CreatePaging(dataList.Rows.Count, page, "Index.aspx?current=0&type=search&keyword=" + searchKeyword);
                break;

            case "articleTag":
                try
                {
                    articleTag = Request["articleTag"].ToString();
                }
                catch (Exception ex)
                {
                    articleTag = "1";
                }
                sqlString = "SELECT TOP " + __PageSize + " * FROM Article WHERE ID NOT IN (SELECT TOP " + topValue + " ID FROM Article WHERE Flag=" + (int)ArticleFlag.normal + " AND ArticleTag=" + articleTag + " ORDER BY SetTop, CreateTime DESC, ID DESC) AND Flag=" + (int)ArticleFlag.normal + " AND ArticleTag=" + articleTag + " ORDER BY SetTop, CreateTime DESC, ID DESC";
                string articleTagCount = "SELECT ID FROM Article WHERE Flag=" + (int)ArticleFlag.normal + " AND ArticleTag=" + articleTag;
                dataList = SqlHelper.GetDataTable(articleTagCount);
                this.paging.InnerHtml = CreatePaging(dataList.Rows.Count, page, "Index.aspx?current=0&type=articleTag&articleTag=" + articleTag);
                break;

            case "archives":
                try
                {
                    archives = Request["archives"].ToString();
                }
                catch (Exception ex)
                {
                    archives = DateTime.Now.ToString();
                }
                sqlString = "SELECT TOP " + __PageSize + " * FROM Article WHERE ID NOT IN (SELECT TOP " + topValue + " ID FROM Article WHERE Flag=" + (int)ArticleFlag.normal + " AND CreateTime>='" + Convert.ToDateTime(archives).ToString() + "' AND CreateTime<'" + Convert.ToDateTime(archives).AddMonths(1).ToString() + "' ORDER BY SetTop, CreateTime DESC, ID DESC) AND Flag=" + (int)ArticleFlag.normal + " AND CreateTime>='" + Convert.ToDateTime(archives).ToString() + "' AND CreateTime<'" + Convert.ToDateTime(archives).AddMonths(1).ToString() + "' ORDER BY SetTop, CreateTime DESC, ID DESC";
                string archivesCount = "SELECT ID FROM Article WHERE Flag=" + (int)ArticleFlag.normal + " AND CreateTime>='" + Convert.ToDateTime(archives).ToString() + "' AND CreateTime<'" + Convert.ToDateTime(archives).AddMonths(1).ToString() + "'";
                dataList = SqlHelper.GetDataTable(archivesCount);
                this.paging.InnerHtml = CreatePaging(dataList.Rows.Count, page, "Index.aspx?current=0&type=archives&archives=" + archives);
                break;
        }
        #endregion

        dataList = SqlHelper.GetDataTable(sqlString);
        this.rptDataList.DataSource = dataList;
        this.rptDataList.DataBind();
    }
}